package com.lqian.simplestructure.linkedlist;

/**
 * 循环链表是头尾相连的链表，最后一个指针的next指向头指针，形成一个环
 */
public class LoopNode {
    int data;

    LoopNode next = this;

    public LoopNode(int data) {
        this.data = data;
    }

    //插入一个节点做为当前节点的下一个节点
    public void after(LoopNode node){
        LoopNode nextNode = this.next;
        this.next = node;
        this.next.next = nextNode;//node.next = nextNode;

    }


    //删除下一个节点
    public  void removeNext(){
        LoopNode nextNext = this.next.next;
        this.next = nextNext;
    }

    //获取下一个节点
    public LoopNode next(){
        return this.next;
    }
    //获取节点中的数据
    public  int getData(){
        return this.data;
    }

    public static void main(String[] args) {
        LoopNode n1 = new LoopNode(1);
        LoopNode n2 = new LoopNode(2);
        LoopNode n3 = new LoopNode(3);
        LoopNode n4 = new LoopNode(4);
        //增加节点
        n1.after(n2);
        n2.after(n3);
        n3.after(n4);
        System.out.println(n1.next().getData());
        System.out.println(n2.next().getData());
        System.out.println(n3.next().getData());
        System.out.println(n4.next().getData());


    }
}
